package com.class05.service.impl;

import com.class05.mapper.ScheduleMapper;
import com.class05.pojo.Schedule;
import com.class05.service.ScheduleService;
import com.class05.utils.PageBean;
import com.class05.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/22 16:00
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Override
    public R page(int pageSize, int currentPage) {
        // 分页
        PageHelper.startPage(currentPage,pageSize);
//        sql limit x,y;
//        查询
        List<Schedule> schedules = scheduleMapper.queryList();
        //分页数据组装
        PageInfo<Schedule> pageInfo = new PageInfo<>(schedules);
        PageBean<Schedule> pageBean = new PageBean<>
                (currentPage,pageSize,pageInfo.getTotal(),pageInfo.getList());
        R ok = R.ok(pageBean);
        return ok;

    }

    @Override
    public R remove(Integer id) {
        int row = scheduleMapper.deleteById(id);
        if (row>0){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R save(Schedule schedule) {
        int row = scheduleMapper.insert(schedule);
        if (row>0){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
        if (schedule.getId() == null){
            return R.fail("核心参数id为空，无法修改！");
        }
        int row = scheduleMapper.update(schedule);
        if (row>0){
            return R.ok(null);
        }
        return R.fail(null);
    }
}
